import { message } from 'antd';
export default {
  namespace: 'logger',

  state: {
    loggerFiles: [], // 日志文件,所有的日志文件,显示.顺序是按照日期倒叙排列.
    loggerInfo: [], // 日志显示的内容,如果是按照id号的时候,是要单独显示这个id的日志文件
    debugIdList: [], // debug文件的所有的id号
    debugInfoEndFlag: false, // 日志是否已经到了要显示的日志列表的结尾
    lineImageIdListShow: [], // 线阵相机的图片id,要展示的id,前100张图片.
    lineImagePreviewModalVisible: false, // 线阵图片预览框是否显示
    lineImagePreviewSrc: null, // 线阵图片预览的时候展示的图片
    lineImagePreviewName: '', // 文件名称
    produceAmountData: [],
    produceDayAmountData: [], // 日产量展示的表格内容.
    cutterImageIdListShow: [], // 铡刀相机的图片,要展示的id,前100张图片.
    cutterImagePreviewModalVisible: false, // 铡刀的预览的时候展示的图片
    cutterImagePreviewSrc: null, // 铡刀预览对话框展示的图片内容
    cutterImagePreviewName: '', // 铡刀预览的文件名称
  },

  // 和后台交互
  effects: {
    *getProduceAmountRecord({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_produce_amount_record',
        []
      );
      yield put({
        type: 'updateProduceAmountRecord',
        payload: response,
      });
    },
    *openCutterImageDir({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_open_cutter_image_dir',
        []
      );
      if (response == 'ok') {
        message.success('目录打开成功!', 3);
      } else {
        message.error('目录打开失败,' + response, 5);
      }
    },

    *openLineImageDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_open_line_image_dir', []);
      if (response == 'ok') {
        message.success('目录打开成功!', 3);
      } else {
        message.error('目录打开失败,' + response, 5);
      }
    },

    *getLineImageDirBoardList({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_line_image_dir_board_list',
        [payload]
      );
      yield put({
        type: 'updateLineImageDirBoardList',
        payload: response,
      });
    },

    *getCutterImageDirBoardList({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_cutter_image_dir_board_list',
        [payload]
      );
      yield put({
        type: 'updateCutterImageDirBoardList',
        payload: response,
      });
    },
    *getLoggerInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_get_logger_info', [
        payload,
      ]);
      yield put({
        type: 'updateLoggerInfo',
        payload: response,
      });
    },

    *updateLoggerInfoByBoardIndex({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_logger_info_by_board_index',
        [payload]
      );
      yield put({
        type: 'updateLoggerInfoOnly',
        payload: response,
      });
    },

    *openDebugDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_open_debug_dir', []);
      if (response == 'ok') {
        message.success('目录打开成功!', 3);
      } else {
        message.error('目录打开失败,' + response, 5);
      }
    },

    *updateLoggerInfoByHourTime({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_logger_info_by_hour_time',
        [payload]
      );
      yield put({
        type: 'updateLoggerInfoOnly',
        payload: response,
      });
    },

    *searchCutterImageIndex({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_search_cutter_image_index',
        [payload]
      );
      yield put({
        type: 'updateCutterImageDirBoardList',
        payload: response,
      });
    },

    *searchDebugIdValue({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_search_debug_id_list', [
        payload,
      ]);
      yield put({
        type: 'updateDebugIdList',
        payload: response,
      });
    },

    *searchImageIndex({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_search_image_index', [
        payload,
      ]);
      yield put({
        type: 'updateLineImageDirBoardList',
        payload: response,
      });
    },

    *preViewLineImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_line_image_pre_view', [
        payload,
      ]);
      yield put({
        type: 'updateLineImageSrc',
        payload: response,
      });
    },

    *preViewCutterImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_cutter_image_pre_view', [
        payload,
      ]);
      yield put({
        type: 'updateCutterImageSrc',
        payload: response,
      });
    },

    *nextCutterImageSrc({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_next_cutter_image_src',
        []
      );
      yield put({
        type: 'updateCutterImageSrc',
        payload: response,
      });
    },

    *lastCutterImageSrc({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_last_cutter_image_src',
        []
      );
      yield put({
        type: 'updateCutterImageSrc',
        payload: response,
      });
    },

    *nextLineImageSrc({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_next_line_image_src', []);
      yield put({
        type: 'updateLineImageSrc',
        payload: response,
      });
    },

    *lastLineImageSrc({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_last_line_image_src', []);
      yield put({
        type: 'updateLineImageSrc',
        payload: response,
      });
    },
    *openFileSaveDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_open_file_save_dir', []);
      if (response == 'ok') {
        message.success('目录打开成功!', 3);
      } else {
        message.error('目录打开失败,' + response, 5);
      }
    },

    *clearFileSaveDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'hdlt', 'web_request_clear_file_save_dir', []);
      if (response == 'ok') {
        message.success('清空目录成功!', 3);
      } else {
        message.error('清空失败,' + response, 5);
      }
    },

    *copyLineImageToFileSave({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_copy_file_to_file_save_dir',
        [payload]
      );
      if (response === 'ok') {
        message.success('复制成功!', 3);
      } else {
        message.error('复制失败,' + response, 5);
      }
    },

    *copyCutterImageToFileSave({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_copy_cutter_file_to_file_save_dir',
        [payload]
      );
      if (response === 'ok') {
        message.success('复制成功!', 3);
      } else {
        message.error('复制失败,' + response, 5);
      }
    },

    *nextDebugInfoPage({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_next_debug_info_page',
        []
      );
      yield put({
        type: 'updateLoggerInfoOnly',
        payload: response,
      });
    },
    *updateLoggerInfoByKeyWord({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'hdlt',
        'web_request_get_logger_info_by_key_word',
        [payload]
      );
      yield put({
        type: 'updateLoggerInfoOnly',
        payload: response,
      });
    },
  },

  // 状态更新
  reducers: {
    updateProduceAmountRecord(state, action) {
      return {
        ...state,
        produceAmountData: action.payload[0],
        produceDayAmountData: action.payload[1],
      };
    },
    updateCutterImageDirBoardList(state, action) {
      return {
        ...state,
        cutterImageIdListShow: action.payload,
      };
    },

    updateDebugIdList(state, action) {
      return {
        ...state,
        debugIdList: action.payload,
      };
    },

    updateLineImageDirBoardList(state, action) {
      return {
        ...state,
        lineImageIdListShow: action.payload,
      };
    },

    updateLineImageSrc(state, action) {
      let imageData;
      if (action.payload[1] == null) {
        imageData = null;
      } else {
        imageData = 'data:image/jpeg;base64,' + action.payload[1];
      }
      return {
        ...state,
        lineImagePreviewModalVisible: action.payload[0],
        lineImagePreviewSrc: imageData,
        lineImagePreviewName: action.payload[2],
      };
    },

    updateCutterImageSrc(state, action) {
      let imageData;
      if (action.payload[1] == null) {
        imageData = null;
      } else {
        imageData = 'data:image/jpeg;base64,' + action.payload[1];
      }
      return {
        ...state,
        cutterImagePreviewModalVisible: action.payload[0],
        cutterImagePreviewSrc: imageData,
        cutterImagePreviewName: action.payload[2],
      };
    },

    updateLoggerInfo(state, action) {
      return {
        ...state,
        loggerFiles: action.payload[0],
        loggerInfo: action.payload[1],
        debugIdList: action.payload[2],
        debugInfoEndFlag: action.payload[3],
      };
    },
    updateLoggerInfoOnly(state, action) {
      return {
        ...state,
        loggerInfo: action.payload[0],
        debugInfoEndFlag: action.payload[1],
      };
    },
  },
};
